package shixun.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import shixun.domain.Root;
import shixun.domain.User;
import shixun.service.accountInfService;
import shixun.service.UserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private accountInfService accountInfService;


    @RequestMapping("/index")
    public String index(HttpSession session, Model model, HttpServletRequest request){
        User user = (User)session.getAttribute("loginUser");
        if (user==null){
            model.addAttribute("msg","请重新登录");
            return "login";
        }else {
            double currentMoney = accountInfService.getCurrentMoney(user.getId(),0);
            session.setAttribute("currentMoney",currentMoney);
            String currentCredit=null;
            String preparedBack=null;
            int creditNumber = accountInfService.findNumber(user.getId(),1);
            if (creditNumber>0){
                double credit = accountInfService.getCurrentMoney(user.getId(), 1);
                if (credit<0){
                    double overMoney = -credit;
                    session.setAttribute("overMoney",overMoney);
                }
                currentCredit = String.valueOf(credit);
                session.setAttribute("currentCredit",currentCredit);
               double back =  20000-credit;
                if (back<0){
                    back = 0;
                }
                preparedBack = String.valueOf(back);
                session.setAttribute("preparedBack",preparedBack);
            }else{
                  session.setAttribute("currentCredit",currentCredit);
                  session.setAttribute("preparedBack",preparedBack);
            }
            return "index";
        }
    }

    @RequestMapping("/rootIndex")
    public String rootIndex(HttpSession session){
        List<User> users = userService.findAllUsers();
        session.setAttribute("users",users);
        return "rootIndex";
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(String username, String password, Model model, HttpSession session) {
        User user = userService.login(username, password);
        session.setAttribute("loginUser",user);
        if (user==null){
            model.addAttribute("msg","用户名或密码错误！");
            return "login";
        }else if("N".equals(user.getStatus())){
            model.addAttribute("msg","该账户已被冻结，请联系管理员！");
            return "login";
        }else{
            return "redirect:/index";
        }
    }

    @RequestMapping(value = "/rootLogin",method = RequestMethod.POST)
    public String rootLogin(String username, String password, Model model, HttpSession session) {
        Root root = userService.rootLogin(username, password);
        session.setAttribute("loginUser",root);
        if (root==null){
            model.addAttribute("msg","用户名或密码错误！");
            return "rootLogin";
        }
        return "redirect:/rootIndex";
    }

    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public String register(String username,String password,String phone,int type){
        if (!userService.findUser(username)){
            User user = new User();
            user.setUsername(username);
            user.setPassword(password);
            user.setPhone(phone);
            user.setStatus("Y");
            userService.add(user);
        }
        int id = userService.findIdByUsername(username);
        String information = "id为"+id+"的用户注册了一个账户";
        if (type==0){
            accountInfService.insertInformation(information,id,0,0);
        }else{
            accountInfService.insertInformation(information,id,20000,1);
        }
        return "login";
    }

    @RequestMapping("/cancellation")
    public String cancellation(HttpSession session){
        User user = (User) session.getAttribute("loginUser");
        userService.cancellation(user.getId());
        return "login";
    }

    @RequestMapping("/forget")
    private String forget(String username,String password){
        userService.update(username,password);
        return "login";
    }

    @RequestMapping("/frozen")
    private String frozen(int id){
        userService.frozen(id);
        return "redirect:/rootIndex";
    }

    @RequestMapping("/thaw")
    private String thaw(int id){
        userService.thaw(id);
        return "redirect:/rootIndex";
    }

    @RequestMapping("/jumpRegister")
    public String jumpRegister(){
        return "register";
    }

    @RequestMapping("/jumpLogin")
    public String jumpLogin(){
        return "login";
    }

    @RequestMapping("/jumpRootLogin")
    public String jumpRootLogin(){
        return "rootLogin";
    }

    @RequestMapping("/jumpForget")
    public String jumpForget(){
        return "forget";
    }

    @RequestMapping("/jumpRegistered")
    public String jumpRegistered(){
        return "registered";
    }

}
